import pygame

class Image(pygame.sprite.Sprite):
    def __init__(self, pathFmt, pathIdx, pos, size = None, pathIdxCnt = 0):
        self.pathFmt = pathFmt
        self.pathIdx = pathIdx
        self.pos = list(pos)
        self.size = size
        self.pathIdxCnt = pathIdxCnt
        self.updateImage()

    def updateImage(self):
        # 图像加载与放缩适配屏幕
        path = self.pathFmt
        if (self.pathIdxCnt != 0):
            path = path % self.pathIdx
            # print(path)
        self.image = pygame.image.load(path)
        if (self.size):
            self.image = pygame.transform.scale(self.image, self.size)

    def updateSize(self, size):
        self.size = size
        self.updateImage()

    def updateIdx(self, pathIdx):
        self.pathIdx = pathIdx
        self.updateImage()

    def getRect(self):
        rect = self.image.get_rect()
        rect.x, rect.y = self.pos
        return rect
    
    def doDirection(self):
        self.pos[0] -= 1;
    
    def draw(self, screen):
        screen.blit(self.image, self.getRect())